package vip.web3.admin.sys.controller.biz;

import com.alibaba.excel.EasyExcel;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import vip.web3.admin.sys.controller.base.AbstractController;
import vip.web3.admin.sys.entity.biz.AOrderEntity;
import vip.web3.admin.sys.form.user.OrderPageForm;
import vip.web3.admin.sys.service.biz.IAOrderService;
import vip.web3.admin.sys.vo.biz.export.ExOrderVo;
import vip.web3.common.utils.DateUtils;
import vip.web3.common.utils.PageUtils;
import vip.web3.common.utils.R;
import vip.web3.common.utils.RedisUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 订单Controller
 * <p>
 * aut-horlilei
 *
 * @date 2024-09-27
 */
@RestController
@RequestMapping("/sys/order")
public class AOrderController extends AbstractController {

    @Autowired
    private IAOrderService aOrderService;



    /**
     * 查询订单列表
     */
    @RequiresPermissions("sys:order:list")
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params, @Valid OrderPageForm form) {
        PageUtils page = aOrderService.pageOrders(params, form);
        return R.ok().put("page", page);
    }

    /**
     * 查询订单列表
     */
    @RequiresPermissions("sys:order:list")
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       @RequestParam Map<String, Object> params,
                       @Valid OrderPageForm form) throws IOException {
        List<ExOrderVo> orders = aOrderService.export(params, form);
        if (CollectionUtils.isEmpty(orders)) {
            orders = Collections.EMPTY_LIST;
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("订单数据_" + DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN), "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ExOrderVo.class).sheet("订单").doWrite(orders);

    }

    /**
     * 新增保存订单
     */
    @RequiresPermissions("sys:order:add")
    @PostMapping("/add")
    public R addSave(@Valid @RequestBody AOrderEntity aOrder) {
        aOrderService.save(aOrder);
        return R.ok();
    }

    /**
     * 修改订单
     */
    @RequiresPermissions("sys:order:edit")
    @GetMapping("/get/{id}")
    public R edit(@PathVariable("id") Integer id) {
        AOrderEntity aOrder = aOrderService.getById(id);
        return R.ok();
    }

    /**
     * 修改保存订单
     */
    @RequiresPermissions("sys:order:edit")
    @PostMapping("/edit")
    public R editSave(@Valid @RequestBody AOrderEntity aOrder) {
        aOrderService.updateById(aOrder);
        return R.ok();
    }

    /**
     * 删除订单
     */
    @RequiresPermissions("sys:order:remove")
    @PostMapping("/remove")
    public R remove(String ids) {
        aOrderService.deleteAOrderByIds(ids);
        return R.ok();
    }

    @PostMapping("/pass/off")
    public R off(@RequestBody Integer id) {
        AOrderEntity order = aOrderService.getById(id);
        int active = order.getActive();
        int leftTimes = order.getLeftTimes();
        if (leftTimes > 0) {
            if (active == 0) {
                order.setActive(1);
            } else {
                order.setActive(0);
            }
            aOrderService.updateById(order);
        }
        return R.ok();
    }
}
